/* 
 * File: AccelerometerHandler
 * Created: 13-JUN-2011
 * Author: Colin Gall-McDaid
 * 
 * Description: A handler for the Android accelerometer. Stores three members, holding
 * the acceleration on each of the three accelerometer axes.
 */
package com.gallsoft.games.framework.impl;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccelerometerHandler implements SensorEventListener {

    private float accelX;
    private float accelY;
    private float accelZ;

    /**
     * Constructor. Takes an Android context from which it gets a SensorManager
     * instance to set up the event listening.
     * 
     * @param context Android context
     */
    public AccelerometerHandler(Context context) {
        SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

        if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
            Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
            manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
        }
    }

    /**
     * Not used
     */
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // do nothing
    }

    /**
     * Fetch the acceleromter values from the provided SensorEvent and store in the
     * handlers members.
     * 
     * @param event the sensor event
     */
    public void onSensorChanged(SensorEvent event) {
        accelX = event.values[0];
        accelY = event.values[1];
        accelZ = event.values[2];
    }

    /**
     * Get the acceleration on the x-axis
     * 
     * @return x-axis acceleration
     */
    public float getAccelX() {
        return accelX;
    }
    
    /**
     * Get the acceleration on the y-axis
     * 
     * @return y-axis acceleration
     */
    public float getAccelY() {
        return accelY;
    }
    
    /**
     * Get the acceleration on the z-axis
     * 
     * @return z-axis acceleration
     */
    public float getAccelZ() {
        return accelZ;
    }
    
}
